package com.tjetc.service.impl;

import com.tjetc.dao.CommonQuestionMapper;
import com.tjetc.domain.ChatHistory;
import com.tjetc.domain.CommonQuestion;
import com.tjetc.repository.CommonQuestionDao;
import com.tjetc.service.CommonQuestionService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommonQuestionServiceImpl implements CommonQuestionService {
    @Resource
    private CommonQuestionDao commonQuestionDao;
    @Resource
    private CommonQuestionMapper commonQuestionMapper;
    @Override
    public List<CommonQuestion> findAll() {
        List<CommonQuestion> list=new ArrayList<>();
        for (CommonQuestion commonQuestion : commonQuestionDao.findAll()) {
            list.add(commonQuestion);
        }
        return list;

    }

    @Override
    public int add(String xuzhi) {
        CommonQuestion commonQuestion = new CommonQuestion();
        commonQuestion.setXuzhi(xuzhi);
        if (commonQuestionMapper.insert(commonQuestion)==1){

            try {
                commonQuestionDao.save(commonQuestion);

            } catch (Exception e) {
                commonQuestionMapper.del(commonQuestion.getId());
                e.printStackTrace();
                return 0;
            }
            return 1;
        }else {
            return  0;
        }

    }

    @Override
    public int del(Integer id) {
       if (commonQuestionMapper.del(id)==1){
           try {
               commonQuestionDao.deleteById(id);

           } catch (Exception e) {
               return 0;

           }
           return 1;
           }else {
           return 0;
       }
       }

    @Override
    public boolean update(CommonQuestion commonQuestion) {
        if (commonQuestionMapper.update(commonQuestion)==1){
            try {
                commonQuestionDao.save(commonQuestion);

            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
            return true;
        }else {
            return false;
        }

    }

}
